package com.llc925.cst.myrecycleview_1.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


import com.llc925.cst.myrecycleview_1.R;
import com.llc925.cst.myrecycleview_1.activity.MainActivity;
import com.llc925.cst.myrecycleview_1.activity.RegisterActivity;
import com.llc925.cst.myrecycleview_1.bean.UserBean;
import com.llc925.cst.myrecycleview_1.model.UserModel;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2017/4/16.
 */

public class LoginActivity extends AppCompatActivity {
    private EditText userpass;
    private Button btn_login;
    private Button btn_register;
    private EditText username;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initViews();
        initEvents();
        initData();
    }

    public void initViews() {
        btn_login = (Button) findViewById(R.id.btn_login);
        userpass = (EditText) findViewById(R.id.password);
        username = (EditText) findViewById(R.id.username);
    }

    public void initEvents() {
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UserModel userModel = new UserModel();
                Call call = userModel.login(username.getText().toString(), userpass.getText().toString());
                Callback<UserBean> callback = new Callback<UserBean>() {
                    @Override
                    public void onResponse(Call<UserBean> call, Response<UserBean> response) {
                        UserBean UserBean = response.body();
                        if (!"0".equals(UserBean.getUserid())) {
                            Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                            startActivity(intent);
                            finish();
                        } else
                            Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onFailure(Call<UserBean> call, Throwable t) {
                    }
                };
                call.enqueue(callback);
            }

            ;
        });
        btn_register = (Button) findViewById(R.id.btn_register);
        btn_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });
    }

    public void saveUser(String user_id) {
        SharedPreferences sharedPreferences = getSharedPreferences("userbean", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();//获取编辑器
        editor.putString("username", username.getText().toString());
        int userid = Integer.parseInt(user_id);
        editor.putInt("user_id", userid);
        editor.putString("userpass", userpass.getText().toString());
        editor.commit();
    }

    public void initData() {

    }
}